package net.huashitong.interview.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.interview.dao.RegisterDao;
import net.huashitong.interview.entity.InterviewFillUnit;
import net.huashitong.interview.entity.InterviewRegister;
import net.huashitong.interview.filter.InterviewRegisterFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RegisterService extends StringPKService<InterviewRegister> {
    @Autowired
    private RegisterDao dao;
    @Autowired
    private SysTableService tableService;
    @Autowired
    private FillUnitService fillUnitService;

    @Override
    protected BaseDao<InterviewRegister, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(InterviewRegister obj) {
        obj.setSysId(tableService.updateMaxSysId("t_gis_interviewRegister", null));
    }

    public List<InterviewRegister> queryRegister(InterviewRegisterFilter filter) {
        return dao.queryRegister(filter);
    }

    public String getNewFillDate(){
        return dao.getNewFillDate();
    }
//    public List<InterviewRegister> searchRegister(InterviewRegisterFilter filter){
//        return dao.queryRegister(filter);
//    }

    public List<Map<String, Object>> baseResult(List<InterviewRegister> list) {
        List<Map<String, Object>> result = new ArrayList<>();
        //封装    政府单位
        List<InterviewFillUnit> fillUnits = fillUnitService.query();
        Map<String,String> unitMap = new HashMap<>();
        for(InterviewFillUnit fillUnit : fillUnits){
            unitMap.put(fillUnit.getSysId(),fillUnit.getFillUnitName());
        }

        List<String> strList = new ArrayList<>();
        for (InterviewRegister register : list) {
            if (!strList.contains(register.getUserName())) {
                strList.add(register.getUserName());
            }
        }
        for (String str : strList) {
            Map<String, Object> map = new HashMap<>();
            int count = 0;
            map.put("userName", str);
            List<Map<String, String>> mapList = new ArrayList<>();
            int allNeedInterview = 0;
            int interviewed = 0;
            for (InterviewRegister register : list) {
                Map<String, String> map2 = new HashMap<>();

                if (str.equals(register.getUserName())) {
                    if(ObjectUtils.isNotBlank(register.getBusinessProblem())&&!"无".equals(register.getBusinessProblem())){
                        map2.put("haveProblem","1");
                    }else{
                        map2.put("haveProblem","0");
                    }
                    allNeedInterview++;
                    if(ObjectUtils.isNotBlank(register.getInterviewCountThisMonth())&&!"0".equals(register.getInterviewCountThisMonth())){
                        interviewed++;
                    }
                    count += Integer.parseInt(ObjectUtils.isBlank(register.getInterviewCountThisMonth()) ? "0" : register.getInterviewCountThisMonth());
                    map2.put("business",register.getContactBusiness());
                    map2.put("interviewCount",ObjectUtils.isBlank(register.getInterviewCountThisMonth())?"0":
                            ("0".equals(register.getInterviewCountThisMonth())?"0":
                                    register.getInterviewCountThisMonth()));
                    map2.put("sysId",register.getSysId());
                    map2.put("unitId",register.getFillUnitId());
                    map2.put("unitName",unitMap.get(register.getFillUnitId()));
                    mapList.add(map2);
                }
            }
            map.put("allNeedInterview",allNeedInterview);
            map.put("interviewed",interviewed);
            map.put("allInterviewCount", count==0?"0":count);
            map.put("interviewDetails",mapList);
            result.add(map);
        }
        return result;
    }
}
